/**
 * Copyright (C) 2014 xspace app Project.
 */

package com.xspace.android.xafcommon.been;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


/**
 * @version V1.0.0
 * @ClassName: AppConfigBean
 * @author:mawen
 * @Date：2014-2-27
 * @Description: app_config.xml 解析
 */
public class AppConfigBean {
    public static Map<String, String> sCls = new HashMap<String, String>();

    static {
        sCls.put("wnx", "cn.xspace.app.wnx.activity.WnxMainAct");
        sCls.put("nxb", "cn.xspace.app.nxb.main.activity.NxbMainAct");
        sCls.put("qac", "cn.xspace.app.qac.HomeQACTabHostAct");
        sCls.put("appstore", "cn.xspace.app.appstore.HomeAppStoreTabHostAct");
        sCls.put("exweb", "cn.xspace.app.exweb.activity.ExWebAct");
        sCls.put("gqxx", "cn.xspace.app.spdm.HomeSPDMTabHostAct");
        sCls.put("nmg", "cn.xspace.app.nmg.HomeNMGTabHostAct");
        sCls.put("law", "cn.xspace.app.law.HomeLawTabHostAct");
    }

    public String appVersion = "";
    /**
     * App版本号
     **/
    public String appID = "";
    /**
     * grid:九宫格，sidebar:底部栏，bottombar:侧边栏
     */
    public String layoutID = "";
    /**
     * 0:...，1:...，2:...
     */
    public String skinID = "";
    /**
     * 机构名称
     */
    public String departmentName = "";
    /**
     * 机构id
     */
    public String departmentID = "";
    /**
     * 根据用户查询到的机构id
     */
    public String departmentIDByUser = "";
    /**
     * 根据用户查询到的机构名称
     */
    public String departmentNameByUser = "";
    public String access_token = "";
    public String isfarmer = "";
    public String app_dept_areacode = "";
    public String user_dept_areacode = "";
    /**
     * 根据用户所在的部门id
     */
    public String user_deptid = "";
    public String user_deptname = "";
    public String user_deptcode = "";

    public String WXAppId = "";
    public String WXAppSecret = "";
    public String BaiDuAppKey = "";


    /**
     * 背景图片
     */
    public String backgroundPicture = "";
    /**
     * 区分重庆畜牧
     **/
    public String platCode = "";
    /**
     * 频道列表
     */
    public List<Menu> menus = new ArrayList<Menu>();
    /**
     * 新增参数
     */
    public List<BBizList> bizList;
    public String user_zhencode;
    public String user_zhenname;
    public String user_cuncode;
    public String user_cunname;
    public String user_sheid;
    public String user_shename;

    /**
     * @param @param inStream
     * @return
     * @throws
     * @Description: 解析AppConfigBean
     */
    public static AppConfigBean parseAppConfigBean(InputStream inStream) {
        AppConfigBean appConfigBean = new AppConfigBean();
        List<Menu> menus = new ArrayList<Menu>();
        DocumentBuilderFactory factory = null;
        DocumentBuilder builder = null;
        try {
            factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
            Document document = builder.parse(inStream);
            Element element = document.getDocumentElement();
            NodeList channelNodes = element.getElementsByTagName("AppID");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.appID = childNodes.item(j).getNodeValue();

                }
            }

            NodeList wxAppIdNodes = element.getElementsByTagName("WXAppId");
            for (int i = 0; i < wxAppIdNodes.getLength(); i++) {
                Element channelElement = (Element) wxAppIdNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.WXAppId = childNodes.item(j).getNodeValue();

                }
            }
            NodeList baiduKeyNodes = element.getElementsByTagName("BaiduAppKey");
            for (int i = 0; i < baiduKeyNodes.getLength(); i++) {
                Element channelElement = (Element) baiduKeyNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.BaiDuAppKey = childNodes.item(j).getNodeValue();

                }
            }

            NodeList wxAppSecretNodes = element.getElementsByTagName("WXAppSecret");
            for (int i = 0; i < wxAppSecretNodes.getLength(); i++) {
                Element channelElement = (Element) wxAppSecretNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.WXAppSecret = childNodes.item(j).getNodeValue();

                }
            }
            channelNodes = element.getElementsByTagName("LayoutID");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.layoutID = childNodes.item(j).getNodeValue();
                }
            }
            channelNodes = element.getElementsByTagName("SkinID");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.skinID = childNodes.item(j).getNodeValue();
                }
            }
            channelNodes = element.getElementsByTagName("DepartmentName");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.departmentName = childNodes.item(j)
                            .getNodeValue();
                }

            }

			/*
             * gongpiwei 获取App版本号
			 * 2014-05-14
			 */
            channelNodes = element.getElementsByTagName("AppVersion");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.appVersion = childNodes.item(j)
                            .getNodeValue();
                }

            }
            channelNodes = element.getElementsByTagName("PlatCode");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.platCode = childNodes.item(j)
                            .getNodeValue();
                }

            }

            channelNodes = element.getElementsByTagName("DepartmentId");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
//                    appConfigBean.departmentID = childNodes.item(j)
//                            .getNodeValue();
                }

            }

            channelNodes = element.getElementsByTagName("BackgroundPicture");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                Element channelElement = (Element) channelNodes.item(i);
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    appConfigBean.backgroundPicture = childNodes.item(j)
                            .getNodeValue();
                }
            }

            channelNodes = element.getElementsByTagName("Menu");
            for (int i = 0; i < channelNodes.getLength(); i++) {
                // 循环获取索引为i的channel节点
                Element channelElement = (Element) channelNodes.item(i);
                Menu menu = new Menu();
                menu.isAndroidApp = "0";
                menu.isInstall = "1";
                NodeList childNodes = channelElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    int nodeType = childNodes.item(j).getNodeType();
                    if (nodeType == Node.ELEMENT_NODE) {
                        String nodeName = childNodes.item(j).getNodeName();

                        Node node = childNodes.item(j).getFirstChild();
                        String nodeValue = (node == null ? "" : node
                                .getNodeValue());

                        if ("MenuIndex".equals(nodeName)) {
                            menu.menuIndex = "0"/*nodeValue*/;
                        } else if ("MenuKey".equals(nodeName)) {

                            String value = sCls.get("exweb");
                            menu.isShowOnHomePage = "1";
                            if (sCls.keySet().contains(nodeValue)) {
                                value = sCls.get(nodeValue);
                            }
                            if (nodeValue.equals("exweb")) {
                                menu.isShowOnHomePage = "0";
                            }
                            menu.packageName = value;
                            menu.menuKey = childNodes.item(j).getChildNodes().item(0).getNodeValue();
                        } else if ("MenuName".equals(nodeName)) {
                            menu.menuName = nodeValue;
                        } else if ("MenuID".equals(nodeName)) {
                            menu.menuID = nodeValue;
                        } else if ("MenuURL".equals(nodeName)) {
                            menu.menuURL = nodeValue;
                        } else if ("MenuImg".equals(nodeName)) {
                            menu.menuImg = nodeValue;
                        } else if ("MenuImgSelected".equals(nodeName)) {
                            menu.menuImgSelected = nodeValue;
                        }
                    }
                }
                menus.add(menu);
            }

            appConfigBean.menus = menus;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }



        return appConfigBean;
    }


}
